using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace ASPNETVN.PORTAL.Modules.DVDZone.Components
{
	public partial class Customer
	{
		#region Private members.
		
		protected int _ID;
		protected string _EmailAddress = string.Empty;
		protected string _Password = string.Empty;
		protected string _FullName = string.Empty;
		protected string _AvatarUrl = string.Empty;
		protected bool _Gender;
		protected string _Address = string.Empty;
		protected string _City = string.Empty;
		protected string _CountryCode = string.Empty;
		protected string _Phone = string.Empty;
		protected DateTime _JoinedDate;

		#endregion
		
		//---------------------------------------------------------------------------------------------

		#region Properties.
		
		public int ID
		{
			set {this._ID = value;}
			get {return this._ID;}
		}
		
		public string EmailAddress
		{
			set {this._EmailAddress = value;}
			get {return this._EmailAddress;}
		}
		
		public string Password
		{
			set {this._Password = value;}
			get {return this._Password;}
		}
		
		public string FullName
		{
			set {this._FullName = value;}
			get {return this._FullName;}
		}
		
		public string AvatarUrl
		{
			set {this._AvatarUrl = value;}
			get {return this._AvatarUrl;}
		}
		
		public bool Gender
		{
			set {this._Gender = value;}
			get {return this._Gender;}
		}
		
		public string Address
		{
			set {this._Address = value;}
			get {return this._Address;}
		}
		
		public string City
		{
			set {this._City = value;}
			get {return this._City;}
		}
		
		public string CountryCode
		{
			set {this._CountryCode = value;}
			get {return this._CountryCode;}
		}
		
		public string Phone
		{
			set {this._Phone = value;}
			get {return this._Phone;}
		}
		
		public DateTime JoinedDate
		{
			set {this._JoinedDate = value;}
			get {return this._JoinedDate;}
		}
		
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static Customer Load(int iD)
		{
			const string spName = "[dbo].[p_DVDZONE_Customer_Load]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, iD);
			Customer entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
			if (reader.Read())
			{
				entity = new Customer();
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("EmailAddress"))) entity.EmailAddress = reader.GetString(reader.GetOrdinal("EmailAddress"));
				if (!reader.IsDBNull(reader.GetOrdinal("Password"))) entity.Password = reader.GetString(reader.GetOrdinal("Password"));
				if (!reader.IsDBNull(reader.GetOrdinal("FullName"))) entity.FullName = reader.GetString(reader.GetOrdinal("FullName"));
				if (!reader.IsDBNull(reader.GetOrdinal("AvatarUrl"))) entity.AvatarUrl = reader.GetString(reader.GetOrdinal("AvatarUrl"));
				if (!reader.IsDBNull(reader.GetOrdinal("Gender"))) entity.Gender = reader.GetBoolean(reader.GetOrdinal("Gender"));
				if (!reader.IsDBNull(reader.GetOrdinal("Address"))) entity.Address = reader.GetString(reader.GetOrdinal("Address"));
				if (!reader.IsDBNull(reader.GetOrdinal("City"))) entity.City = reader.GetString(reader.GetOrdinal("City"));
				if (!reader.IsDBNull(reader.GetOrdinal("CountryCode"))) entity.CountryCode = reader.GetString(reader.GetOrdinal("CountryCode"));
				if (!reader.IsDBNull(reader.GetOrdinal("Phone"))) entity.Phone = reader.GetString(reader.GetOrdinal("Phone"));
				if (!reader.IsDBNull(reader.GetOrdinal("JoinedDate"))) entity.JoinedDate = reader.GetDateTime(reader.GetOrdinal("JoinedDate"));
			}
			reader.Close();
			dbCommand.Connection.Close();
			
			return entity;
		}		
		
		//---------------------------------------------------------------------------------------------
		

		public static DataSet SelectAll()
        {
            const string spName = "[dbo].[p_DVDZONE_Customer_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------

		public static IDataReader SelectReaderAll()
        {
            const string spName = "[dbo].[p_DVDZONE_Customer_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_DVDZONE_Customer_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------

		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_DVDZONE_Customer_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public static int Insert(string emailAddress, string password, string fullName, string avatarUrl, bool gender, string address, string city, string countryCode, string phone, DateTime joinedDate)
		{
			Customer entity = new Customer();			
			entity.EmailAddress = emailAddress;
			entity.Password = password;
			entity.FullName = fullName;
			entity.AvatarUrl = avatarUrl;
			entity.Gender = gender;
			entity.Address = address;
			entity.City = city;
			entity.CountryCode = countryCode;
			entity.Phone = phone;
			entity.JoinedDate = joinedDate;
			return entity.Insert();
		}
		
		public int Insert()
		{
			return this.Insert(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public int Insert(SqlTransaction transaction)
		{			
			const string spName = "[dbo].[p_DVDZONE_Customer_Insert]";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
			db.AddInParameter(dbCommand, "@EmailAddress", SqlDbType.NVarChar, this._EmailAddress);
			db.AddInParameter(dbCommand, "@Password", SqlDbType.NVarChar, this._Password);
			db.AddInParameter(dbCommand, "@FullName", SqlDbType.NVarChar, this._FullName);
			db.AddInParameter(dbCommand, "@AvatarUrl", SqlDbType.NVarChar, this._AvatarUrl);
			db.AddInParameter(dbCommand, "@Gender", SqlDbType.Bit, this._Gender);
			db.AddInParameter(dbCommand, "@Address", SqlDbType.NVarChar, this._Address);
			db.AddInParameter(dbCommand, "@City", SqlDbType.NVarChar, this._City);
			db.AddInParameter(dbCommand, "@CountryCode", SqlDbType.VarChar, this._CountryCode);
			db.AddInParameter(dbCommand, "@Phone", SqlDbType.VarChar, this._Phone);
			db.AddInParameter(dbCommand, "@JoinedDate", SqlDbType.DateTime, this._JoinedDate);
			
			if (transaction != null)
			{
				db.ExecuteNonQuery(dbCommand, transaction);
				this._ID = (int) db.GetParameterValue(dbCommand, "@ID");
				return this._ID;
			}
            else
			{
				db.ExecuteNonQuery(dbCommand);
				this._ID = (int) db.GetParameterValue(dbCommand, "@ID");
				return this._ID;
			}			
		}
		
		//---------------------------------------------------------------------------------------------
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.
		
		public static int Update(int iD, string emailAddress, string password, string fullName, string avatarUrl, bool gender, string address, string city, string countryCode, string phone, DateTime joinedDate)
		{
			Customer entity = new Customer();			
			entity.ID = iD;
			entity.EmailAddress = emailAddress;
			entity.Password = password;
			entity.FullName = fullName;
			entity.AvatarUrl = avatarUrl;
			entity.Gender = gender;
			entity.Address = address;
			entity.City = city;
			entity.CountryCode = countryCode;
			entity.Phone = phone;
			entity.JoinedDate = joinedDate;
			return entity.Update();
		}
		
		public int Update()
		{
			return this.Update(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Update(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_DVDZONE_Customer_Update]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);
			db.AddInParameter(dbCommand, "@EmailAddress", SqlDbType.NVarChar, this._EmailAddress);
			db.AddInParameter(dbCommand, "@Password", SqlDbType.NVarChar, this._Password);
			db.AddInParameter(dbCommand, "@FullName", SqlDbType.NVarChar, this._FullName);
			db.AddInParameter(dbCommand, "@AvatarUrl", SqlDbType.NVarChar, this._AvatarUrl);
			db.AddInParameter(dbCommand, "@Gender", SqlDbType.Bit, this._Gender);
			db.AddInParameter(dbCommand, "@Address", SqlDbType.NVarChar, this._Address);
			db.AddInParameter(dbCommand, "@City", SqlDbType.NVarChar, this._City);
			db.AddInParameter(dbCommand, "@CountryCode", SqlDbType.VarChar, this._CountryCode);
			db.AddInParameter(dbCommand, "@Phone", SqlDbType.VarChar, this._Phone);
			db.AddInParameter(dbCommand, "@JoinedDate", SqlDbType.DateTime, this._JoinedDate);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int Delete(int iD)
		{
			Customer entity = new Customer();
			entity.ID = iD;
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.Delete(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Delete(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_DVDZONE_Customer_Delete]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		#endregion
	}	
}